package com.wc.alorithm_luogu.P4913;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author congge
 * @Date 2023/5/22 14:55
 * @description 用队列弄二叉树，好像挺新奇的
 */
public class Main2 {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int nextInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int) in.nval;
    }

    public static void main(String[] args) {
        int n = nextInt();
        Node[] nodes = new Node[n + 1];
        for (int i = 1; i <= n; i++) {
            nodes[i] = new Node(nextInt(), nextInt());
        }

        Queue<Integer> queue = new LinkedList<>();

        queue.add(1);

        int ans = 0;

        int tmp;
        while (!queue.isEmpty()){
            int size = queue.size();
            ans++;
            for (int i = 0; i < size; i++) {
                tmp = queue.poll();
                if (nodes[tmp].left!=0){
                    queue.add(nodes[tmp].left);
                }
                if (nodes[tmp].right!=0){
                    queue.add(nodes[tmp].right);
                }
            }
        }
        System.out.println(ans);
    }

    static class Node {
        int left, right;

        public Node(int left, int right) {
            this.left = left;
            this.right = right;
        }
    }
}
